import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../home.dart';
import 'widgets/home_widgets.dart';
import '../../../widgets/widgets.dart';
import '../../../routes/routes.dart';
import '../../../shared/shared.dart';

class MessagesTab extends GetView<HomeLogic> {
  const MessagesTab({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Column(
        children: [
          _buildHeader(),
          CommonWidget.line(),
          _buildSearch(),
          CommonWidget.line(),
          FutureBuilder(
              future: mockNetworkData(),
              builder: (BuildContext context, AsyncSnapshot snapshot) =>
                  _messageCard())
        ],
      ),
    );
  }

  Future<String> mockNetworkData() async {
    return Future.delayed(const Duration(milliseconds: 1000), () => "我是数据！");
  }

  Expanded _messageCard() {
    return Expanded(
      child: ScrollConfiguration(
        behavior: MyBehavior(),
        child: Obx(() => ListView.builder(
            itemCount: controller.userMessages.length,
            itemBuilder: (BuildContext context, int index) => Obx(() =>
                MessageCard(
                    userMessage: controller.userMessages[index],
                    index: index)))),
      ),
    );
  }

  Row _buildSearch() {
    return Row(children: [
      Expanded(
          child: Container(
              margin: const EdgeInsets.all(10), child: const Search())),
      Container(
          margin: const EdgeInsets.all(10),
          child: IconFont(IconNames.provider, size: 22))
    ]);
  }

  Row _buildHeader() {
    return Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
              margin: const EdgeInsets.symmetric(vertical: 10, horizontal: 10),
              child: const HomeAvatar(imageURL: AssetsURL.headURL)),
          const Text('消息',
              style: TextStyle(color: ColorConstants.themeColor, fontSize: 20)),
          Container(
              margin: const EdgeInsets.symmetric(vertical: 10, horizontal: 10),
              child: IconButton(
                  onPressed: () => Get.toNamed(Routes.setting),
                  icon: IconFont(IconNames.setting, size: 40)))
        ]);
  }
}
